<?php

namespace Home\Controller;
use Common\Controller\AppframeController;
class WeixinController extends AppframeController {
	
	protected $db;
	function _initialize(){
		parent::_initialize();
		
		vendor('weixin.JsApiPay');
		vendor('weixin.WxPayConfig');
		vendor('weixin.WxPayApi');
		vendor('weixin.PayNotifyCallBack');
		
	}




    
 public function notify()
    {


		$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
		$result = \WxPayResults::Init($xml);
//



	 F("WEI_CHONGZHI",$result);

		if($result[return_code]=="SUCCESS"){






			$order=D("Order")->where(array("order_sn"=>$result[out_trade_no]))->find();


			if($order['status']=="1"&&$result[total_fee]/100 == $order["price_sum"]){


				if($order["modal"]=="product"||$order["modal"]=="huodong"){



						$s = array(
							"status" => 2,
							"pay_time" => time(),
							"payment_notify_id" => $result[transaction_id],
						);


						D("Order")->where(array("order_sn" => $result[out_trade_no]))->save($s);
//
//					$content=array(
//
//						"first"=>array(
//							"value"=>"恭喜你购买成功!",
//						),
//						"keyword1"=>array(
//							"value"=>"微信支付",
//						),
//
//						"keyword2"=>array(
//							"value"=>$order['title'],
//						),
//						"keyword3"=>array(
//							"value"=>$order['order_sn'],
//						),
//						"keyword4"=>array(
//							"value"=>"您购买的商品已经支付成功，查看详情了解更多信息",
//						),
//						"remark"=>array(
//							"value"=>"",
//						)
//
//
//					);
//
//
//
//
//
//
//
//
//					$ss=$this->wk->sendTplMsg($result["openid"],"3Nt_aKpXXupHz9IiWTDpGYtd2g2f7DstkuO5gR_Oz7s",U('User/Order/show',array('order_sn'=>$order['order_sn'])),$content);








				}



				if($order["modal"]=="chongzhi"){


					D("Order")->where(array("order_sn"=>$result[out_trade_no]))->save(array("status"=>"2"));


					$n=D("Users")->chongzhi($order["uid"],$result[total_fee]/100);

					$gz=D("CzGz")->where(array("price"=>$order["price_sum"]))->find();

					if($gz["num"]){


						$data = array(
							"ip" => get_client_ip(),
							"inputtime" => time(),
							"money" =>$result[total_fee]/100,
							"uid" => $order["uid"],
							"product_name" => $gz["product_name"],
							"num" => $gz["num"],
							"cishu" => $gz["cishu"],
							"yue" => $n,
							"type" => "充值",
							"next_time"=>time(),
							"pay_time"=>time()
						);

						D("QbLog")->add($data);


						D("Users")->where(array("id"=>$order["uid"]))->save(array("vip"=>1));



						D("Users")->fanxian($order["order_sn"],$gz["bili"]);



					}




				}





				$result=array(
					"return_code"=>"SUCCESS",
					"return_msg"=>"OK",

				);

				echo "";


			}
			else{

				$result=array(
					"return_code"=>"FAIL",

				);

				echo "";
			}





		}
		else{
			$result=array(
				"return_code"=>"FAIL",

			);

			echo "";


		}










    }


    public function  result(){
    	
    	$result=I("get.result")?I("get.result"):0;
    	
    	
    	if($result){
    		$this->error("支付完成");
    		
    	}
    	else{
    		$this->error("支付失败");
    		
    	}
    	
    	
    	
    }
    

    
    
	}
 ?>